<?php
/*
Plugin Name: WP-Book Library
Version: 1.0-alpha
Plugin URI: http://myanmapress.com
Description: Manage Book library.
Author: Sithu Thwin
Author URI: http://myanmapress.com
 */
 if(defined('WPBL_VERSION')) return;
define('WPBL_VERSION', '1.0-alpha');
define('WPBL_PLUGIN_PATH', dirname(__FILE__));
define('WPBL_PLUGIN_FOLDER', basename(WPBL_PLUGIN_PATH));

if(defined('WP_ADMIN') && defined('FORCE_SSL_ADMIN') && FORCE_SSL_ADMIN){
    define('WPBL_PLUGIN_URL', rtrim(str_replace('http://','https://',get_option('siteurl')),'/') . '/'. PLUGINDIR . '/' . basename(dirname(__FILE__)) );
}else{
    define('WPBL_PLUGIN_URL', rtrim(get_option('siteurl'),'/') . '/'. PLUGINDIR . '/' . basename(dirname(__FILE__)) );
}

add_action('admin_menu', 'addWPBLSubmenu');
function addWPBLSubmenu()
{
        add_submenu_page('post-new.php', 'Submit Book', 'Submit Book', 10, __FILE__, 'bookSubmit');
}

function bookSubmit (){

        if(isset($_POST['submit'])){
                $ok = 1;
                if($_POST['bookid'] == ''){
                        $ok = 0;
                        $check_bookid = 1;
                }

                if($_POST['title'] == ''){
                        $ok = 0;
                        $check_title = 1;
                }

                if($ok){
                        $my_post = array();
                        $my_post['post_title'] = $_POST['title'];
                        $my_post['post_content'] = $_POST['description'];
                        $my_post['post_status'] = 'publish';
                        $my_post['tags_input'] = $_POST['tags'];
                        $my_post['post_category'] = array($_POST['category']);

                        $id = wp_insert_post($my_post);
                        if($id){
						add_post_meta($id, 'bookid', $_POST['bookid']);
                        add_post_meta($id, 'bookauthors',$_POST['bookauthors']);
                        add_post_meta($id, 'book-thumb',$_POST['thumb']);

?>
                        <div class="wrap"><h2>&nbsp</h2><div class="updated" id="message" style="background-color: rgb(255, 251, 204);"><p><strong>Book was added successfully.</strong></div></div>
                        <?php
                        }else{
?>
                        <div class="wrap"><h2>&nbsp</h2><div class="updated" id="message" style="background-color: rgb(255, 251, 204);"><p><strong>There was an error when adding the book.</strong></div></div>
                        <?php
                        }
                        return;
                }
        }

?>
<div class="wrap">
<table cellpadding="5" cellspacing="10">
<tr>
<td width="20%" valign="top">
</td>
<td valign="top" align="left">

</td> 
</tr>
</table>
</div>
<?php
        if(isset($_POST['submit'])){
                if($_POST['bookid'] == ''){
                        echo '<div class="updated" id="message" style="background-color: rgb(255, 251, 204);"><p><strong>You should enter a valid url</strong></div>';
                }

                if($_POST['title'] == ''){
                        echo '<div class="updated" id="message" style="background-color: rgb(255, 251, 204);"><p><strong>You should enter a title</strong></div>';
                }
	}

?>
<form name="post" method="post">

<div class="wrap">
<h2>Submit a New Book to Your Website</h2>
<div id="poststuff">
<div id="post-body">

<div class="postbox">
<h3>Book Title:</h3>
<div class="inside">
<input type="text" name="title" size="30" tabindex="1" value="<?php echo htmlspecialchars($_POST['title']);?>" id="title"  style="width: 98%;"/>
</div>
</div>

<div class="postbox">
<h3>Book Description:</h3>
<div class="inside">
<textarea id="description" tabindex="6" name="description" cols="40" rows="2" style="width: 98%;"/>
<?php echo htmlspecialchars($_POST['description']);?>
</textarea>
</div>
</div>

<div class="postbox">
<h3><label for="title">Book Category:</label></h3>
<div class="inside">
<?php wp_dropdown_categories( array( 'hide_empty' => 0, 'name' => 'category', 'orderby' => 'id', 'hierarchical' => 1, 'tab_index' => 3 ) ); ?>
</div>
</div>

<div class="postbox">
<h3>Tags: <small>seperated by comma's</small></h3>
<div class="inside">
<input type="text" name="tags" size="30" tabindex="1" value="<?php echo htmlspecialchars($_POST['tags']);?>" id="tags"  style="width: 98%;"/>
</div>
</div>

<div class="postbox">
<h3>Thumbnail URL: <small>To find out the URL of a thumbnail image right click on the thumbnail and scroll down to properties, the URL will be under location.</small></h3>
<div class="inside">
<input type="text" name="thumb" size="30" tabindex="1" value="<?php echo htmlspecialchars($_POST['thumb']);?>" id="thumb"  style="width: 98%;"/>
</div>
</div>

<div class="postbox">
<h3>BookISBN or ID: <small>book id!</small></h3>
<div class="inside">
<input type="text" id="bookid" tabindex="6" name="bookid" cols="40" rows="6" style="width: 98%;"/>
<?php echo htmlspecialchars($_POST['bookid']);?>
</input>
</div>
</div>

<div class="postbox">
<h3>Book Author: <small>Book Authors!</small></h3>
<div class="inside">
<input type="text" name="bookauthors" size="30" tabindex="1" value="<?php echo htmlspecialchars($_POST['bookauthors']);?>" id="bookauthors"  style="width: 98%;"/>
</div>
</div>

</div>
</div>

<input type="hidden" name="submit" value="submit">
<input type="submit" name="submit" value="Submit">
</form>
<?php
}
function widget_wpbl_search($args) {
	extract($args); 
	echo $before_widget;
			echo $args['before_title'];
	_e('Search Books','AWK');
			echo $args['after_title'];
	if(function_exists('wp_custom_fields_search')) 
	wp_custom_fields_search(); 
	echo $after_widget; 
}
register_sidebar_widget(__('Search Books','AWK'), 'widget_wpbl_search');
?>